package com.runner.suporte;

import java.util.Calendar;

import android.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class RegistrarExercicioActivity extends Activity{
	private Calendar dateAndTime = Calendar.getInstance();
	private String stData;
	private String stTempo;
	private EditText edKM;
	private RadioButton rbCaminhada;
	private TextView lbData;
	private TextView lbTempo;
	
	private DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
				stData = Funcoes.corrNumero(dayOfMonth) + "/" + Funcoes.corrNumero(monthOfYear) + "/"+year;
				acertaData();
		}};
		
		private TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				stTempo = "" + Funcoes.corrNumero(hourOfDay)+":"+ Funcoes.corrNumero(minute);
				acertaHora();
			}};
			
			@Override
			
			public void onCreate(Bundle icicle){
				super.onCreate(icicle);
				setContentView(R.layout.registrar_exercicio);
				lbData = (TextView)findViewById(R.id.lbData);
				edKM = (EditText) findViewById(R.id.edKM);
				rbCaminhada = (RadioButton)findViewById(R.id.rbCaminhada);
				findViewById(R.id.btData).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
				new DatePickerDialog(RegistrarExercicioActivity.this, d,
				dateAndTime.get(Calendar.YEAR),
				dateAndTime.get(Calendar.MONTH),
				dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
						
					}
				});
			}
			@Override
			public boolean onCreateOptionsMenu(Menu menu){
				MenuInflater inflater = getMenuInflater();
				inflater.inflate(R.menu.registrar, menu);
				return true;
			}
			public boolean OnOptionsItemSelected(MenuItem item){
				switch (item.getItemId()) {
				case R.id.itConfirmar: // Confirmar
					acaoConfirmar();
					return true;
				case R.id.itVisualizar: //Visualizar
					acaoVisualizar();
					return true;
				}
				return false;
			}
			private void acaoConfirmar(){
				DataHelper dh = new DataHelper(this);
				byte tipo = (byte)((rbCaminhada.isChecked())?0:1);
				dh.inserir(new Exercicio(stData, stTempo, Float.parseFloat(edKM.getText().toString()), tipo));
				edKM.setText("");
				Toast.makeText(this, getResources(), getString(R.string.regcad),Toast.LENGTH_SHORT).show();
				dh.close();
			}
			private void acaoVisualizar(){
				startActivity(new Intent(RegistrarExercicioAcitivity.this, MostrarExerciciosActivity.class));
			}
			public void acertaData(){
				lbData.setText(getResources().getString(R.string.data)+ ""+ stData);
			}	
			public void acertaHora(){
				lbTempo.setText(getResource().getString(R.string.tempo) + "" + stTempo);
			}
}
